using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PocketLibrary.Storage;
using PocketSettings;
using System.Threading;

namespace PocketApps
{
    public partial class BoxNetRestForm : Form
    {

        BoxNetAPI bxApi = new BoxNetAPI();
        PocketSettings.PocketSettings settings = PocketSettings.PocketSettings.Create();
        Stack<string> breadCrumbs = new Stack<string>();
        //Queue<string> breadCrumbs = new Queue<string>();


        public BoxNetRestForm()
        {
            InitializeComponent();
        }

        private void closeBoxNetRestForm(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BoxNetRestForm_Load(object sender, EventArgs e)
        {
            bxApi.OnLogin += new BoxNetApiResponseHandler(bxApi_OnLogin);
            bxApi.OnTreeResponse += new BoxNetApiTreeHandler(bxApi_OnTreeResponse);
            bxApi.OnDownload += new BoxNetApiResponseHandler(bxApi_OnDownload);
            bxApi.OnDownloadProgress += new PocketLibrary.UploadProgressHandler(bxApi_OnDownloadProgress);
            bxApi.OnUploadProgress += new PocketLibrary.UploadProgressHandler(bxApi_OnUploadProgress);
            Thread login = new Thread(LoginToBoxNet);
            login.Start();
        }

        void bxApi_OnUploadProgress(object sender, PocketLibrary.UploadProgressEventArgs e)
        {
            progressBar1.BeginInvoke(new EventHandler(delegate
            {
                if (e.Complete)
                {
                    progressBar1.Visible = false;
                    progressBar1.Value = 0;
                }
                else
                {
                    progressBar1.Visible = true;
                    progressBar1.Value = e.CurrentProgress;
                }
            }));
        }

        void bxApi_OnDownloadProgress(object sender, PocketLibrary.UploadProgressEventArgs e)
        {
            progressBar1.BeginInvoke(new EventHandler(delegate
            {
                if (e.Complete)
                {
                    progressBar1.Visible = false;
                    progressBar1.Value = 0;
                }
                else
                {
                    progressBar1.Visible = true;
                    progressBar1.Value = e.CurrentProgress;
                }
            }));
        }

        void bxApi_OnDownload(object sender, BoxNetApiResponseArgs args)
        {
            //MessageBox.Show(args.Response);
            progressBar1.BeginInvoke(new EventHandler(delegate
            {
                progressBar1.Visible = false;
            }));
        }

        void bxApi_OnTreeResponse(object sender, BoxNetApiTreeArgs args)
        {
            liBoxNet.BeginInvoke(new EventHandler(delegate
            {

                liBoxNet.Items.Clear();

                for (int i = 0; i < args.TreeItems.Count; i++)
                {
                    ListViewItem item = new ListViewItem();
                    item.SubItems.Add(args.TreeItems[i].ItemType.ToString());
                    item.SubItems.Add(args.TreeItems[i].ItemId);

                    switch (args.TreeItems[i].ItemType)
                    {
                        case BoxNetAPITreeItem.TreeItemType.Parent:
                            item.Text = "[" + args.TreeItems[i].Name + "]";
                            break;
                        case BoxNetAPITreeItem.TreeItemType.Folder:
                            item.Text = "[" + args.TreeItems[i].Name + "]";
                            break;
                        case BoxNetAPITreeItem.TreeItemType.File:
                            item.Text = args.TreeItems[i].Name;
                            item.SubItems.Add(args.TreeItems[i].FileSize.ToString());
                            break;
                        default:
                            break;
                    }

                    liBoxNet.Items.Add(item);
                }
            }));



        }

        void bxApi_OnLogin(object sender, BoxNetApiResponseArgs args)
        {
            if (args.Response == "logged")
            {
                //breadCrumbs.Push("0");
                bxApi.ListFolder();
            }
            else
            {
                MessageBox.Show("Login not successful");
            }
        }

        private void LoginToBoxNet()
        {
            try
            {
                bxApi.Login(settings.SelectedUserProfile.Settings.Get("boxNetUsername"),
                    settings.SelectedUserProfile.Settings.Get("boxNetPassword"));
            }
            catch (ThreadAbortException)
            {
            }
        }

        private void listView1_ItemActivate(object sender, EventArgs e)
        {
            if (liBoxNet.SelectedIndices.Count > 0)
            {
                string itemtext = liBoxNet.Items[liBoxNet.SelectedIndices[0]].Text;
                string itemid = liBoxNet.Items[liBoxNet.SelectedIndices[0]].SubItems[2].Text;
                string itemtype = liBoxNet.Items[liBoxNet.SelectedIndices[0]].SubItems[1].Text;

                if (itemtype == "Folder")
                {
                    // list folder
                    //Thread itemListFolder = new Thread(bxApi.ListFolder(itemid));
                    //itemListFolder.IsBackground = true;
                    //itemListFolder.Start();
                    //breadCrumbs.Push(itemid);

                    bxApi.ListFolder(itemid);
                }
                else if (itemtype == "Parent")
                {
                    //breadCrumbs.Pop();
                    //bxApi.ListFolder(breadCrumbs.Peek());
                    bxApi.ListParentFolder();
                }
                else
                {
                    // download file
                    using (SaveFileDialog saveDlg = new SaveFileDialog())
                    {
                        saveDlg.FileName = itemtext;
                        string extension = itemtext.Substring(itemtext.LastIndexOf(".") + 1);
                        saveDlg.Filter = "All files|*.*";
                        if (saveDlg.ShowDialog() == DialogResult.OK)
                        {
                            progressBar1.Visible = true;
                            bxApi.Download(itemid, saveDlg.FileName + extension);
                        }
                    }
                }
            }
        }

        private void liBoxNet_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (liBoxNet.SelectedIndices.Count > 0)
            {
                string itemtype = liBoxNet.Items[liBoxNet.SelectedIndices[0]].SubItems[1].Text;
                if (itemtype == "Folder")
                {
                    miAction.Text = "Open";
                }
                else if (itemtype == "File")
                {
                    miAction.Text = "Download";
                }
                else if (itemtype == "Parent")
                {
                    miAction.Text = "Open";
                }
            }
        }

        private void miUpload_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "All files |*.*|Pictures|*.jpg;*.png;*.bmp;*.gif|Video|*.mpg;*.mp4;*.avi;*.wmv|Music|*.wma;*.mp3";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    bxApi.Upload(ofd.FileName);
                }
            }
        }

        private void miBoxDelete_Click(object sender, EventArgs e)
        {
            if (liBoxNet.SelectedIndices.Count > 0)
            {
                foreach (int index in liBoxNet.SelectedIndices)
                {
                    string itemtext = liBoxNet.Items[index].Text;
                    string itemid = liBoxNet.Items[index].SubItems[2].Text;
                    string itemtype = liBoxNet.Items[index].SubItems[1].Text;
                    if (itemtype == "Folder")
                        bxApi.Delete(BoxNetAPITreeItem.TreeItemType.Folder, itemid);
                    else if (itemtype == "File")
                        bxApi.Delete(BoxNetAPITreeItem.TreeItemType.File, itemid);
                }
            }

        }
    }
}